package com.hexinfo.dmpro.data.transfer.common.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hexinfo.dmpro.common.utils.CommonConstants;
import com.hexinfo.dmpro.data.transfer.common.dao.DateParatitionFieldMatchingMapper;
import com.hexinfo.dmpro.data.transfer.common.model.CdhFieldEntity;
import com.hexinfo.dmpro.data.transfer.common.model.DateParatitionFieldMatching;
import com.hexinfo.dmpro.data.transfer.common.service.IDateParatitionFieldMatchingService;
import org.springframework.stereotype.Service;


import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DateParatitionFieldMatchingService extends ServiceImpl<DateParatitionFieldMatchingMapper, DateParatitionFieldMatching> implements IDateParatitionFieldMatchingService {

    @Override
    public List<DateParatitionFieldMatching> queryList() {
        LambdaQueryWrapper<DateParatitionFieldMatching> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(DateParatitionFieldMatching::getDel, CommonConstants.STATUS_NORMAL);
        return this.baseMapper.selectList(wrapper);
    }

    @Override
    public DateParatitionFieldMatching queryByParatitionList(List<CdhFieldEntity> partitionList) {
        if (CollectionUtil.isNotEmpty(partitionList)) {
            List<DateParatitionFieldMatching> dateParatitionFieldMatchings = queryList();
            Map<String, DateParatitionFieldMatching> dateParatitionFieldMatchingMap = dateParatitionFieldMatchings.stream()
                    .collect(Collectors.toMap(DateParatitionFieldMatching::getTimeFieldKay, s -> s));
            for (CdhFieldEntity cdhFieldEntity : partitionList) {
                if (dateParatitionFieldMatchingMap.containsKey(cdhFieldEntity.getFieldName())) {
                    return dateParatitionFieldMatchingMap.get(cdhFieldEntity.getFieldName());
                }
            }
        }
        return null;
    }

}
